using System;
using System.Collections.Generic;
using System.Globalization;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SAK.UI.WebControls
{
  /// <summary>
  /// Controllo che visualizza la data del giorno
  /// </summary>
  [DefaultProperty("Culture")]
  [ToolboxData("<{0}:Today runat=server></{0}:Today>")]
  public class Today : WebControl
  {
    /// <summary>
    /// Impostazione Cultura della data
    /// </summary>
    [Bindable(true)]
    [Category("Impostazioni data")]
    [DefaultValue("")]
    [Localizable(true)]
    public string Culture
    {
      get
      {
        String s = (String)ViewState["Culture"];
        return ((s == null) ? String.Empty : s);
      }

      set
      {
        ViewState["Culture"] = value;
      }
    }

    /// <summary>
    /// Formato della data
    /// </summary>
    [Bindable(true)]
    [Category("Impostazioni data")]
    [DefaultValue("D")]
    [Localizable(true)]
    public string Format
    {
      get
      {
        String s = (String)ViewState["Format"];
        return ((s == null) ? String.Empty : s);
      }

      set
      {
        ViewState["Format"] = value;
      }
    }

    /// <summary>
    /// Render del controllo
    /// </summary>
    /// <param name="output"></param>
    protected override void RenderContents(HtmlTextWriter output)
    {
      string _f = this.Format;
      if (_f.Length == 0)
        _f = "D";

      if (Culture.Length > 0)
      {
        output.Write(DateTime.Now.ToString(_f, new CultureInfo(this.Culture).DateTimeFormat));
      }
      else
        output.Write(DateTime.Now.ToString(_f));
    }
  }
}
